<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="设备id" prop="deviceId">
						<el-input v-model="form.deviceId" placeholder="请输入设备id" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="IMEI" prop="imei">
						<el-input v-model="form.imei" placeholder="请输入IMEI" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="出库模式 10默认出库；20平级出库" prop="outInventoryType">
						<el-input v-model="form.outInventoryType" placeholder="请输入出库模式 10默认出库；20平级出库" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="通讯费配置(10不需要；20需要)" prop="isCommunicationFees">
						<el-input v-model="form.isCommunicationFees" placeholder="请输入通讯费配置(10不需要；20需要)" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="配置方式(10配送；20代付)" prop="deliveryMethod">
						<el-input v-model="form.deliveryMethod" placeholder="请输入配置方式(10配送；20代付)" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="出库单号" prop="outInventoryNumber">
						<el-input v-model="form.outInventoryNumber" placeholder="请输入出库单号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="出库单位" prop="deptId">
						<el-input v-model="form.deptId" placeholder="请输入出库单位" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="客户单位(被出库的单位)" prop="customerDeptId">
						<el-input v-model="form.customerDeptId" placeholder="请输入客户单位(被出库的单位)" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="平台使用年限" prop="platformFeeYears">
						<el-input v-model="form.platformFeeYears" placeholder="请输入平台使用年限" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="平台到期时间" prop="platformExpireDate">
						<el-input v-model="form.platformExpireDate" placeholder="请输入平台到期时间" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="短信剩余（条）,无限制为 999999" prop="smsRemainNum">
						<el-input v-model="form.smsRemainNum" placeholder="请输入短信剩余（条）,无限制为 999999" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="语音剩余（分钟）无限制为 999999" prop="ttsRemainNum">
						<el-input v-model="form.ttsRemainNum" placeholder="请输入语音剩余（分钟）无限制为 999999" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="是否初始化 (10是；20否)" prop="isInitialize">
						<el-input v-model="form.isInitialize" placeholder="请输入是否初始化 (10是；20否)" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="出库时分配短信数" prop="smsNum">
						<el-input v-model="form.smsNum" placeholder="请输入出库时分配短信数" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="出库时分配语音数" prop="ttsNum">
						<el-input v-model="form.ttsNum" placeholder="请输入出库时分配语音数" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="出库时分配平台年限" prop="platformYears">
						<el-input v-model="form.platformYears" placeholder="请输入出库时分配平台年限" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="BasicInventoryListDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/platform/basicInventoryList';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	deviceId: '',
	imei: '',
	outInventoryType: '',
	isCommunicationFees: '',
	deliveryMethod: '',
	outInventoryNumber: '',
	deptId: '',
	customerDeptId: '',
	platformFeeYears: '',
	platformExpireDate: '',
	smsRemainNum: '',
	ttsRemainNum: '',
	isInitialize: '',
	smsNum: '',
	ttsNum: '',
	platformYears: '',
});

// 定义校验规则
const dataRules = ref({
	deviceId: [{ required: true, message: '设备id不能为空', trigger: 'blur' }],
	imei: [{ required: true, message: 'IMEI不能为空', trigger: 'blur' }],
	outInventoryType: [{ required: true, message: '出库模式 10默认出库；20平级出库不能为空', trigger: 'blur' }],
	isCommunicationFees: [{ required: true, message: '通讯费配置(10不需要；20需要)不能为空', trigger: 'blur' }],
	deliveryMethod: [{ required: true, message: '配置方式(10配送；20代付)不能为空', trigger: 'blur' }],
	outInventoryNumber: [{ required: true, message: '出库单号不能为空', trigger: 'blur' }],
	deptId: [{ required: true, message: '出库单位不能为空', trigger: 'blur' }],
	customerDeptId: [{ required: true, message: '客户单位(被出库的单位)不能为空', trigger: 'blur' }],
	platformFeeYears: [{ required: true, message: '平台使用年限不能为空', trigger: 'blur' }],
	platformExpireDate: [{ required: true, message: '平台到期时间不能为空', trigger: 'blur' }],
	smsRemainNum: [{ required: true, message: '短信剩余（条）,无限制为 999999不能为空', trigger: 'blur' }],
	ttsRemainNum: [{ required: true, message: '语音剩余（分钟）无限制为 999999不能为空', trigger: 'blur' }],
	isInitialize: [{ required: true, message: '是否初始化 (10是；20否)不能为空', trigger: 'blur' }],
	smsNum: [{ required: true, message: '出库时分配短信数不能为空', trigger: 'blur' }],
	ttsNum: [{ required: true, message: '出库时分配语音数不能为空', trigger: 'blur' }],
	platformYears: [{ required: true, message: '出库时分配平台年限不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取basicInventoryList信息
	if (id) {
		form.id = id;
		getbasicInventoryListData(id);
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbasicInventoryListData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
